/**
 * Copyright (c) 2024 Hunan OpenValley Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "TextureRotationUtil.h"

const std::array<float, TEXTURE_COORDINATES_SIZE>
    TextureRotationUtil::TEXTURE_NO_ROTATION = {0.0f, 1.0f, 1.0f, 1.0f,
                                                0.0f, 0.0f, 1.0f, 0.0f};

const std::array<float, TEXTURE_COORDINATES_SIZE>
    TextureRotationUtil::TEXTURE_ROTATED_90 = {1.0f, 1.0f, 1.0f, 0.0f,
                                               0.0f, 1.0f, 0.0f, 0.0f};

const std::array<float, TEXTURE_COORDINATES_SIZE>
    TextureRotationUtil::TEXTURE_ROTATED_180 = {1.0f, 0.0f, 0.0f, 0.0f,
                                                1.0f, 1.0f, 0.0f, 1.0f};

const std::array<float, TEXTURE_COORDINATES_SIZE>
    TextureRotationUtil::TEXTURE_ROTATED_270 = {0.0f, 0.0f, 0.0f, 1.0f,
                                                1.0f, 0.0f, 1.0f, 1.0f};

std::array<float, TEXTURE_COORDINATES_SIZE> TextureRotationUtil::getRotation(Rotation rotation, bool flipHorizontal,
    bool flipVertical)
{
    std::array<float, TEXTURE_COORDINATES_SIZE> rotatedTex;
    std::copy(TEXTURE_NO_ROTATION.begin(), TEXTURE_NO_ROTATION.end(),
              rotatedTex.begin());
    switch (rotation) {
        case Rotation::ROTATION_90:
            std::copy(TEXTURE_ROTATED_90.begin(), TEXTURE_ROTATED_90.end(), rotatedTex.begin());
            break;
        case Rotation::ROTATION_180:
            std::copy(TEXTURE_ROTATED_180.begin(), TEXTURE_ROTATED_180.end(), rotatedTex.begin());
            break;
        case Rotation::ROTATION_270:
            std::copy(TEXTURE_ROTATED_270.begin(), TEXTURE_ROTATED_270.end(), rotatedTex.begin());
            break;
        case Rotation::NORMAL:
            // Default case, no action needed
            break;
    }
    const uint8_t step = 2;
    if (flipHorizontal) {
        for (size_t i = 0; i < rotatedTex.size(); i += step) {
            rotatedTex[i] = flip(rotatedTex[i]);
        }
    }
    if (flipVertical) {
        for (size_t i = 1; i < rotatedTex.size(); i += step) {
            rotatedTex[i] = flip(rotatedTex[i]);
        }
    }
    return rotatedTex;
}

float TextureRotationUtil::flip(float i) { return (i == 0.0f) ? 1.0f : 0.0f; }